package com.cms.recursoshumanos.activity.main;

import com.cms.recursoshumanos.activity.acceso.LoginActivity;
import com.cms.recursoshumanos.activity.pantallas_principal.PrincipalMainMenuActivity;
import com.example.frontendrecursoshumanos.R;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainRecursosHumanosActivity extends Activity
{
	
	private static Context context;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		MainRecursosHumanosActivity.context = getApplicationContext();
	}
	public static Context getAppContext()
	{
		return MainRecursosHumanosActivity.context;
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
	    MenuInflater inflater=getMenuInflater();
	    inflater.inflate(R.menu.menu_backend, menu);
	    return super.onCreateOptionsMenu(menu);
	}

	public boolean isOnline() 
	{
	    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo netInfo = cm.getActiveNetworkInfo();
	    if (netInfo != null && netInfo.isConnectedOrConnecting()) 
	    {
	        return true;
	    }
	    Toast.makeText(getAppContext(),"No Posee Connection", Toast.LENGTH_LONG).show();
	    return false;
	}
	protected void makeCall(String num_tlf) 
	{
	    try 
	    {
	        Intent callIntent = new Intent(Intent.ACTION_CALL);
	        callIntent.setData(Uri.parse("tel:"+num_tlf));
	        startActivity(callIntent);
	    } 
	    catch (ActivityNotFoundException activityException) 
	    {
	         Log.e("helloandroid dialing example", "Call failed", activityException);
	    }
	}
	public void goPaginaPrincipal(Activity elActivity)
    {
    	Intent	intent = new Intent(elActivity, PrincipalMainMenuActivity.class);
		startActivity(intent);
		finish();
    }
	public void onLlamarClick(View v)
	{
		String numero_llamar = ((TextView) v).getText().toString();
		makeCall(numero_llamar);
	}

	public void onEnviarEmailCorredorClick(View v)
	{
		String email_enviar = ((TextView) v).getText().toString();
		sendEmail(email_enviar);
	}
	
	protected void sendEmail(String dir_email) 
	{
		
		try 
		{
			Intent i = new Intent(Intent.ACTION_SEND);
			i.setType("message/rfc822");
			i.putExtra(Intent.EXTRA_EMAIL  , new String[]{dir_email});
			i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
			i.putExtra(Intent.EXTRA_TEXT   , "body of email");
		    startActivity(Intent.createChooser(i, "Send mail..."));
		} 
		catch (android.content.ActivityNotFoundException ex) 
		{
		    Toast.makeText(MainRecursosHumanosActivity.this, "There are no email clients installed.", Toast.LENGTH_LONG).show();
		}
	}
	public void goLogin()
	{
		Intent	intent = new Intent(MainRecursosHumanosActivity.this, LoginActivity.class);
  		
	  	  Bundle bundle = new Bundle();
	  	  bundle.putString("id_acceso", "1");
	  	  intent.putExtras(bundle);
		
	  	  startActivity(intent);
  	  
	  	  finish();
	}
	  @Override
	  public boolean onOptionsItemSelected(MenuItem item) 
	  {
	      switch(item.getItemId())
	      {
		      case R.id.cerrar_session:
		    	  goLogin();
		          break;
		      case R.id.about:
		          break;
	      }
	      return true;
	  }
	  
	

	
}
